<?php

/**
 * page actions.
 *
 * @package    VTNS
 * @subpackage page
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class documentListAction extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeDocumentList(sfWebRequest $request)
  {
	$document_temp = new VTNSDocument();
	$document_temp->document_category_id = $request->getParameter('document_category_id');
	$this->document_form = new AdminDocumentForm($document_temp);
	if ($request->hasParameter('create') && $request->getParameter('create') == 'Create') {
		$res = $request->getParameter('document');
        $this->document_category_id = $res['document_category_id'];
		$this->is_create_action = true;
		if ($res['name'] != '') {
            $this->is_create_name = true;
			$document_temp->name = $res['name'];
			$document_temp->document_category_id = $res['document_category_id'];
            $document_temp->document_category_id;
			$document_temp->description = $res['description'];
			$document_temp->views_count = 0;
			if (isset($res['is_active']))
				$document_temp->is_active = 1;
			else
   	            $document_temp->is_active = 0;
			$document_temp->content = $res['content'];
			$document_temp->created_at = date(DATE_ISO8601);
			$document_temp->updated_at = date(DATE_ISO8601);
			try {
                    foreach($this->getRequest()->getFiles() as $file) {
                        $source = $file['file']['tmp_name'];
                        $fileName = $file['file']['name'];
                        $fileSize = $file['file']['size'];
                        $fileType = $file['file']['type'];
                    }
                    if($fileName != "") {
                        if($fileSize > $document_temp->file_type_uploaded['size'] || $fileSize == 0) {
                            $this->message = "File upload phải nhỏ hơn 2MB";
                        }                          
                        else
                        {
                            $fileExt = trim(strrchr($fileName,'.'));
                            $file_name_uploadeds = $document_temp->file_type_uploaded['name'];
                            $this->message = "Dạng file được upload ";
                            foreach( $file_name_uploadeds as $file_upload) {
                                if( $file_upload == $fileExt ) {
                                    $this->status_upload_file = true;
                                    break;
                                }
                                else  {
                                    $this->status_upload_file = false;
                                    $this->message .= $file_upload;
                                }
                                    
                            }
                          
                            if($this->status_upload_file) {
                                $uploadDir = sfConfig::get("sf_upload_dir");
                                $destination = $uploadDir.'/document/'.$fileName;
                                if( !move_uploaded_file($source,$destination))
                                    $this->message = "upload file không thành công";
                                else {
                                    $this->message = "upload file thành công";
                                    $document_temp->file = $fileName;
                                    $document_temp->file_type = $fileExt;
                                    $fileSize = $fileSize/1024;
                                    $document_temp->file_size = round($fileSize,1);
                                    $document_temp->save();
                                }
                            }
                        }
                  }
			} catch (sfException $e) {
				echo $e->getMessage();
			}
		}
		
	} else {
		
		$q = Doctrine_Query::create()
			->from('VTNSDocument c')
			->where('c.document_category_id = ?', $request->getParameter('document_category_id'));
		$this->document_category_id = $request->getParameter('document_category_id');	
		$res = $request->getParameter('document');
		$q = $this->execFilers($q, $res);
		$this->sort_by = $request->getParameter('sort_by', 'created_at');
    	if ($request->getParameter('sort_order', 1))
        	$this->sort_order = 'desc';
    	else
        	$this->sort_order = 'asc';
			
    	if (Doctrine_Core::getTable('VTNSDocument')->hasField($this->sort_by))
        	$q->orderBy('c.'.$this->sort_by.' '.strtoupper($this->sort_order));
			
		$this->pager = new sfDoctrinePager('VTNSDocument', 20);
		$this->pager->setQuery($q);
    	$this->pager->setPage($request->getParameter('page', 1));
    	$this->pager->init();
		if ($request->isXmlHttpRequest()) {
        	return 'Ajax';
    	}
        else 
            return SfView::SUCCESS;
	}
  }


	private function execFilers($q, $res)
	{
		if ($res['name'] != '')
			$q->addWhere('c.name LIKE "%'.$res['name'].'%"');
		return $q;
	}
}
